<template>
  <div>
    <el-button
      type="success"
      class="addmenbertype"
      @click="dialogFormVisible = true"
      >新建数据类型</el-button
    >
    <el-table :data="memberList" style="width: 1200px">
      <el-table-column label="会员账户类型" width="130">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.account_type }}</span>
        </template>
      </el-table-column>
      <el-table-column label="默认有效期" width="130">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.avaliable_date }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" width="130">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="130">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.date }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后编辑时间" width="170">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.edit_date }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" class="accountOperation">
        <template slot-scope="scope">
          <el-button
            type="success"
            size="mini"
           
            >详情</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleEdit(scope.$index, scope.row)"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
     <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="5"
      style="text-align: center"
      @current-change="changepag"
    >
    </el-pagination>
    <!-- 弹出的框 -->
    <el-dialog
      title="新建账号类型"
      :visible.sync="dialogFormVisible"
      width="500px"
    >
      <el-form :model="form">
        <el-form-item label="会员账号类型" :label-width="formLabelWidth">
          <el-input v-model="form.account_type" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="默认有效期" :label-width="formLabelWidth">
          <el-input v-model="form.avaliable_date" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="创建人" :label-width="formLabelWidth">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary"  @click="sendInfo"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getInfo} from "@/api"
import ajax from "@/api/ajax.js";
export default {
  name: "accounttype",
  data() {
    return {
      memberList: [],
      dialogFormVisible: false,
      total:0,
      form: {
        account_type: "",
        avaliable_date: "",
        username: "",
      },
      formLabelWidth: "120px",
      account:''
    };
  },
  methods: {
     // 分页器回调
    changepag(index) {
      this.getInfoAll(index);
    },
    // 获取后台信息
    async getInfoAll(index = 1) {
      let result = await getInfo();
      // console.log(result);
      if (result.code === 200) {
        this.memberList = result.data.slice((index - 1) * 5, index * 5);
        this.total = result.data.length;
      }
    },
    // 点击修改事件
    handleEdit(index,row){
      this.form.account_type=row.account_type
      this.form.avaliable_date=row.avaliable_date
      this.form.username=row.username
      this.account = row.account
      // console.log(account)
      this.dialogFormVisible=true
      
    },
    // 收集当前页面数据发送请求
    sendInfo() {
      this.dialogFormVisible = false;
      ajax.post("http://localhost:4100/register",{
        account_type:this.form.account_type,
        avaliable_date:this.form.avaliable_date,
        username:this.form.username,
        account:this.account
      }).then(res=>{
        console.log("请求成功!")
        console.log(res.data)
        this.memberList = res.data.slice((index - 1) * 5, index * 5);;
      }).catch(err=>{
        console.log("请求失败!",err)
      })
    },
  },
  mounted() {
    this.getInfoAll();
    // 页面加载便请求数据
    // ajax
    //   .get("http://localhost:4100/accountAll")
    //   .then((res) => {
    //     // console.log("请求成功!");
    //     this.memberList = res.data.data;
    //     // console.log(res.data.data);
    //   })
    //   .catch((err) => {
    //     console.log(err);
    //   });

    // 获取当前日期
    // console.log(this.getNowDate())
    // 获取唯一的id
    // console.log(this.getuuid());
  },
};
</script>

<style lang="less" scoped>
.addmenbertype {
  margin: 10px 0;
}
</style>
